% chaos optimization logistics mapping

clc;
clear;
% setup parameters
K = 300;      % iterations
u = 4;        % chaos control parameter
xmin = -20;
xmax = 20;
D = 10;       % dimension

func = inline('sum(x.^2)','x');
% initialize decision variables and chaos viriables
x = rand(1, D) * (xmax - xmin) + xmin;
cx = zeros(1, D);
gbest = func(x);
% record the best fitness
result(1) = gbest;

% mapping the range of decision variable x to the range of chaotic variable cx
cx = (x - xmin) / (xmax - xmin);

for k = 2:K
    % logistic mapping
    cx = u * cx .* (1 - cx);
    
    % converting the chaotic variables to decision variables
    x_new = xmin + cx * (xmax - xmin);
    
    % evaluating the new solution
    if func(x_new) < func(x)
        gbest = func(x_new);
        x = x_new;
    end
    
    result(k) = gbest;
end
plot(result);